package com.hspedu.generic;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author LiuJinTao
 * @className GenericExercise02
 * @date 2024/06/16 0:58
 * @description TODO
 */
public class GenericExercise02 {
    public static void main(String[] args) {
        ArrayList<Employee> list = new ArrayList<>();
        list.add(new Employee("a", new MyDate(2004, 2, 22), 30000));
        list.add(new Employee("a", new MyDate(2004, 2, 21), 20000));
        list.add(new Employee("c", new MyDate(2006, 4, 23), 40000));

        list.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                int n1 = o1.getName().compareTo(o2.getName());
                if (n1 != 0)  return n1;
                return o1.getBirthday().compareTo(o2.getBirthday());
            }
        });

        System.out.println("list=" + list);
    }
}

class Employee {
    private String name;
    private MyDate birthday;
    private double sal;

    @Override
    public String toString() {
        return "\nEmployee{" +
                "name='" + name + '\'' +
                ", birthday=" + birthday +
                ", sal=" + sal +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public Employee(String name, MyDate birthday, double sal) {
        this.name = name;
        this.birthday = birthday;
        this.sal = sal;
    }
}

class MyDate implements Comparable<MyDate> {
    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public int compareTo(MyDate o) {
        int y1 = year;
        int y2 = o.getYear();
        int m1 = month;
        int m2 = o.getMonth();
        int day1 = day;
        int day2 = o.getDay();
        if (y1 - y2 != 0) return y1 - y2;
        if (m1 - m2 != 0) return m1 - m2;
        return day1 - day2;
    }
}